package ysq.xyg.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.extend.ehcache.EhCacheService;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.shop.dao.ShopBankDao;
import ysq.xyg.shop.model.po.ShopBank;
import ysq.xyg.shop.model.vo.ShopBankVo;
import ysq.xyg.shop.service.ShopBankService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 商城模块-银行service实现
 *
 * @author congcong tao
 * @date 2019-5-22 8:56:47
 */
@Service("shopBankService")
public class ShopBankServiceImpl extends BaseServiceImpl<ShopBank> implements ShopBankService {

    @Autowired
    private EhCacheService ehCacheService;
    @Resource
    private ShopBankDao shopBankDao;

    private ShopBank shopBank;

    @Autowired
    public void setBaseDao(ShopBankDao shopBankDao) {
        this.baseDao = shopBankDao;
    }


    /**
     * 分页查询
     *
     * @param shopBankVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-22 8:56:47
     */
    public ReturnEntity page(ShopBankVo shopBankVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<ShopBank> list = acceptList(shopBankVo);
        PageInfo<ShopBank> page=new PageInfo<ShopBank>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param shopBankVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-22 8:56:47
     */
    public ReturnEntity addOrUpdate(ShopBankVo shopBankVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(shopBankVo.getId())){
            shopBank=new ShopBank(currentUser);
            isUpate=false;
        }else{
            shopBank=selectByPrimaryKey(shopBankVo.getId());
            if(ObjectUtil.isNull(shopBank)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            shopBank.setUpdateTime(new Date());
            shopBank.setUpdateUser(currentUser.getId());
        }
        shopBank.setName(shopBankVo.getName());
        shopBank.setImage(shopBankVo.getImage());
        shopBank.setDescription(shopBankVo.getDescription());
        shopBank.setOrderNum(shopBankVo.getOrderNum());
        shopBank.setStatus(shopBankVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(shopBank);
        }else{
            num=insert(shopBank);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        ehCacheService.evictShopBank();
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-22 8:56:47
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        ShopBank shopBank=selectByPrimaryKey(id);
        if(shopBank!=null){
            shopBank.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            shopBank.setUpdateTime(new Date());
            shopBank.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(shopBank);
            if(num>0){
                ehCacheService.evictShopBank();
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param shopBankVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-5-22 8:56:47
     */
    public List<ShopBank> acceptList(ShopBankVo shopBankVo){
        Example example = new Example(ShopBank.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(shopBankVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",shopBankVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(shopBankVo.getName())){
            criteria.andLike("name","%" + shopBankVo.getName() + "%");
        }
        example.setOrderByClause(" update_time desc");
        return shopBankDao.selectByExample(example);
    }

    @Override
    public ReturnEntity acceptSelect(String code) {
        List<ShopBank> list = acceptList(new ShopBankVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(ShopBank entity : list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
}
